// Copyright (C) joyinter
// All rights reserved.
// 
// Author : Jiang Li
// Date   : 2004-7-14

#include "stdafx.h"
#include ".\clntsocket.h"
#include "thdrecv.h"
#include <common/netmsg/aes.h>

using namespace JoyMessage;

extern CJoyAes s_msgCrypto;

//##ModelId=44C4B5E6033C
class CJoyClientSockData : public CJoySockData
{
public:
	//##ModelId=44C4B5E6033E
    CJoyClientSockData()
    {
        m_bBlocking = true;
    }
	//##ModelId=44C4B5E6033F
    void PostRecvData(CJoySockRecvData* pData);
};
//del by deng
//extern bool JoyProcessGameTransmit(MessageBase* msg);

//##ModelId=44C4B5E600DC
CJoySockData* CJoyClientSocket::OnCreateData()
{
    return new CJoyClientSockData();
}

//##ModelId=44C4B5E6033F
void CJoyClientSockData::PostRecvData(CJoySockRecvData* pData)
{
    void* buf = pData->GetData();
    int size = pData->GetDataSize();

    if(size < sizeof(JoyMessage::MessageBase))
    {
        return;
    }

    JoyMessage::MessageBase* msg = (JoyMessage::MessageBase*)buf;

    if((UINT)size < msg->m_size)
    {
        return;
    }

    if(msg->m_crypto)
    {
        int nCryptoSize = size-sizeof(MessageHeader);
        if((nCryptoSize&0x0000000F) != 0)
        {
            return;
        }
        BYTE* pCryptoBuf = (BYTE*)buf+sizeof(MessageHeader);
        s_msgCrypto.Decrypt(pCryptoBuf, pCryptoBuf, nCryptoSize);
        msg->m_crypto = false;
    }

   //del by deng
   /* if(JoyProcessGameTransmit(msg))
    {
        delete pData;
        return;
    }*/

    __super::PostRecvData(pData);
}

void CJoyClientMsgHandler::OnMsg_Invalid(const MessageBase*)
{
}

//##ModelId=44C4B5E60100
CJoyClientSocket::CJoyClientSocket(void)
{
    m_handler = NULL;
}

//##ModelId=44C4B5E60102
CJoyClientSocket::~CJoyClientSocket(void)
{
}

//##ModelId=44C4B5E60109
bool CJoyClientSocket::ConnectServer(const CString& strIP, UINT nPort)
{
    DisconnectServer();

    if(!Create(SOCK_STREAM, 0))
    {
        return false;
    }

    if(!Connect(strIP, nPort))
    {
        if(WSAEWOULDBLOCK == GetLastError())
        {
            return true;
        }
        return false;
    }
    return true;
}

//##ModelId=44C4B5E6010C
void CJoyClientSocket::DisconnectServer()
{
    if(IsConnected())
    {
        MC_Logout logout;
        SendJoyMsg(&logout);
    }
    Close();
}

//##ModelId=44C4B5E600EA
void CJoyClientSocket::OnConnect(int nErrorCode)
{
    __super::OnConnect(nErrorCode);
    if(m_handler == NULL)
    {
        return;
    }
    m_handler->OnSock_Connect(nErrorCode);
}

//##ModelId=44C4B5E600ED
void CJoyClientSocket::OnClose()
{
    __super::OnClose();
    if(m_handler == NULL)
    {
        return;
    }
    m_handler->OnSock_Close();
}

template<typename MsgType>
void CJoyClientSocket::OnDispatchMessage(const MsgType* msg, void (CJoyClientMsgHandler::*pHandler)(const MsgType*))
{
    if(m_handler == NULL)
    {
        return;
    }

    if(!msg->IsValid())
    {
        m_handler->OnMsg_Invalid(msg);
        return;
    }

    (m_handler->*pHandler)(msg);
}

void CJoyClientSocket::OnJoyMsg(const MessageBase* pMsg)
{
    switch(pMsg->m_type)
    {
#   define JOY_MSG_SERVER_DEF(name) case MST_##name: OnDispatchMessage((MS_##name*)pMsg, &CJoyClientMsgHandler::OnMsg_##name); break;
#   include <common\netmsg\ms_def.h>
    }
}

